package com.imau.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imau.bean.User;
import com.imau.common.entiry.PageQuery;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.common.utils.JSONUtil;
import com.imau.service.UserService;
import com.imau.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserConterllor", value = "/user/*")
    public class UserConterllor extends Baseconterllor {

    public UserConterllor() {

        this.service = new UserServiceImp();
    }

    /**登录请求*/
    public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        //1.获取前端
        BufferedReader reader = request.getReader();

        String jsonObj = reader.readLine();

        //2.转换
        User user = JSONUtil.jsonToEntity(jsonObj,User.class);

        //3.校验用户信息是否正确
        boolean b = ((UserService) service).checkLogin(user);


        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS),new ResultEntity(ResultCode.USERNAMEORPASSWORD_ERROR));
    }

    /**添加用户*/
    public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //1.获取前端
        BufferedReader reader = request.getReader();

        String jsonObj = reader.readLine();

        //2.转换

        //第一步转成map对象 key = >Java类属性 value=>java属性值
        Map<String,Object> map = JSON.parseObject(jsonObj, Map.class);

        //第二步 map转成对应实体类

        User user = BeanUtil.mapToBean(map,User.class,true);


        //第三步 获取角色的主键集合
        List roleIds = (List)map.get("roleIds");


        //3.判断用户名是否存在



        //3.调用服务

        boolean b =  ((UserService) service).saveEntity(user,roleIds);


        //4.响应数据

        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        boolean b = service.removeWithById(id);

        //3.响应数据


        this.responseBody(response,b,new ResultEntity(ResultCode.SUCCESS));

    }


    public void update (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {

        //1.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();

        //2.转换

        //第一步转成map对象 key = >Java类属性 value=>java属性值
        Map<String,Object> map = JSON.parseObject(jsonStr, Map.class);

        //第二步 map转成对应实体类

        User user = BeanUtil.mapToBean(map,User.class,true);


        //第三步 获取角色的主键集合
        List roleIds = (List)map.get("roleIds");


        //3.调用服务

        boolean b = service.updateByEntity(user);
        boolean b1 = ((UserService) service).assigningRoles(user.getId(), roleIds);

        //4.响应数据

        this.responseBody(response,b&b1,new ResultEntity(ResultCode.SUCCESS));


    }


    public void getByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //1.获取前台分页数据
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize =  Integer.valueOf(request.getParameter("pageSize"));

        //2.获取前台的JSON串
        BufferedReader reader = request.getReader();

        String jsonStr = reader.readLine();


        //3.转成对象

        User user = JSONUtil.jsonToEntity(jsonStr,User.class);


        //4.调用service方法添加

        Page<User> page = service.queryPageList(user,new PageQuery(pageNum,pageSize));

        //3.响应数据

        this.responseBody(response,page==null?false:true,new ResultEntity(ResultCode.SUCCESS,page));


    }


    public void getInfo(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {


        //1.获取前台主键id
        String id = request.getParameter("id");

        //2.业务处理

        User user =((UserService)service).queryById(id);

        //3.响应数据


        this.responseBody(response,user!=null?true:false,new ResultEntity(ResultCode.SUCCESS,user));

    }
}
